/**
 * @api post model.
 *
 * User: yunshengzhu
 * Date: 2020/12/1
 * Time: 下午2:44
 */
package model

func GetUserToken(token string) (obj UserDb, err error) {
	return obj, db.Where("token=?", token).Take(&obj).Error
}

func GetUserOpenId(openId string) (obj UserDb, err error) {
	return obj, db.Where("open_id=?", openId).Take(&obj).Error
}

func GetUserId(userId int64) (obj UserDb, err error) {
	return obj, db.Where("user_id=?", userId).Take(&obj).Error
}

func UpdateUserPhone(userId int64, phone string) error {
	return db.Exec(`update t_user set phone=? where user_id=?`, phone, userId).Error
}

func CreateUser(obj UserDb) error {
	return db.Create(&obj).Error
}

func UpdateUserToken(userId int64, token string) error {
	return db.Exec(`update t_user set token=? where user_id=?`, token, userId).Error
}

func UpdateUserGrade(userId int64, grade int64) error {
	return db.Exec(`update t_user set grade=? where user_id=?`, grade, userId).Error
}

func UpdateUserGradePUserId(userId int64, grade, pUserId, expTime int64) error {
	return db.Exec(`update t_user set grade=?,p_user_id=?,exp_time=? where user_id=?`, grade, pUserId, expTime, userId).Error
}

func UpdateUserIdTye(userId int64, typ int) error {
	return db.Exec(`update t_user set typ=? where user_id=?`, typ, userId).Error
}

func GetUserPUserIdCount(pUserId int64) (num int64, err error) {
	count := UserCountDb{}
	return count.Count, db.Where("p_user_id=?", pUserId).Select("count(*) as count").Take(&count).Error
}

func GetUserPUserList(pUserId int64, page, size int) (list []UserDb, err error) {
	list = make([]UserDb, 0)
	return list, db.Where("p_user_id=?", pUserId).Offset((page - 1) * size).Limit(size).Order("create_time desc").Find(&list).Error
}
